package view;

import java.io.*;
import java.util.*;

public class ProductView {
    private static List<String> products = new ArrayList<>();
    private static Map<String, Product> productDetails = new HashMap<>();
    private static final String PRODUCT_FILE = "C:\\Users\\lenovo\\IdeaProjects\\wang-jingbos-father真的最后了\\项目源码\\src\\products.txt";

    public ProductView() {
        loadProducts(); // Load products from file on initialization
    }

    public static List<String> getProducts() {
        return new ArrayList<>(products);
    }

    public static void displayMenu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("1. 添加商品");
            System.out.println("2. 修改商品库存");
            System.out.println("3. 查询商品库存");
            System.out.println("4. 返回主菜单");
            System.out.print("Choose an option: ");

            int option = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (option) {
                case 1:
                    addProduct(scanner);
                    break;
                case 2:
                    updateStock(scanner);
                    break;
                case 3:
                    checkStock(scanner);
                    browseProducts();
                    break;
                case 4:
                    return;
                default:
                    System.out.println("Invalid option. Please try again.");
            }
        }
    }

    private static void addProduct(Scanner scanner) {
        System.out.print("添加产品名字: ");
        String productName = scanner.nextLine();
        System.out.print("添加商品价格: ");
        double price = scanner.nextDouble();
        System.out.print("添加商品库存数量：");
        int stock = scanner.nextInt();
        scanner.nextLine();  // Consume newline

        productDetails.put(productName, new Product(price, stock));
        String productInfo = productName + " - ￥" + price + ", 库存: " + stock;
        products.add(productInfo);

        saveProductToFile(productName, price, stock);
        System.out.println("Product added successfully.");

        // Notify OrderView about the new product
        OrderView.updateProductList(products);
    }

    private static void updateStock(Scanner scanner) {
        System.out.print("Enter product name: ");
        String productName = scanner.nextLine();
        System.out.print("Enter new stock: ");
        int stock = scanner.nextInt();
        scanner.nextLine();  // Consume newline

        Product product = productDetails.get(productName);
        if (product != null) {
            product.setStock(stock);
            updateProductList(productName, product);
            checkStockLevel(productName);
        } else {
            System.out.println("Product not found.");
        }
    }

    private static void checkStock(Scanner scanner) {
        System.out.print("Enter product name: ");
        String productName = scanner.nextLine();
        checkStockLevel(productName);
    }

    private static void checkStockLevel(String productName) {
        Product product = productDetails.get(productName);
        if (product != null) {
            System.out.println("Current stock for " + productName + ": " + product.getStock() + ", Price: ￥" + product.getPrice());
            if (product.getStock() < 5) {
                System.out.println("Warning: Stock is low for " + productName + ".");
            }
        } else {
            System.out.println("Product not found.");
        }
    }

    private static void browseProducts() {
        System.out.println("商品列表:");
        for (int i = 0; i < products.size(); i++) {
            System.out.println((i + 1) + ". " + products.get(i));
        }
    }

    private static void saveProductToFile(String productName, double price, int stock) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(PRODUCT_FILE, true))) {
            writer.write(productName + " " + price + " " + stock);
            writer.newLine();
        } catch (IOException e) {
            System.out.println("无法保存商品数据: " + e.getMessage());
        }
    }

    private static void loadProducts() {
        File file = new File(PRODUCT_FILE);
        if (file.exists()) {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] parts = line.split(" ");
                    if (parts.length >= 3) {
                        String productName = parts[0];
                        double price = Double.parseDouble(parts[1]);
                        int stock = Integer.parseInt(parts[2]);
                        productDetails.put(productName, new Product(price, stock));
                        products.add(productName + " - ￥" + price + ", 库存: " + stock);
                    }
                }
            } catch (IOException e) {
                System.out.println("无法读取商品数据: " + e.getMessage());
            }
        }
    }

    private static void updateProductList(String productName, Product product) {
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).startsWith(productName)) {
                products.set(i, productName + " - ￥" + product.getPrice() + ", 库存: " + product.getStock());
                break;
            }
        }
    }

    private static class Product {
        private double price;
        private int stock;

        public Product(double price, int stock) {
            this.price = price;
            this.stock = stock;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public int getStock() {
            return stock;
        }

        public void setStock(int stock) {
            this.stock = stock;
        }
    }
}
